package com.longcai.controller.customer;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.bean.ApiResult;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.entity.customer.ClyjUserTouch;
import com.longcai.service.customer.ClyjUserTouchService;
import com.longcai.util.TokenUtils;
import com.longcai.vo.ClyjGoodsOrderDetailsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 邀请关系表 前端控制器
 * </p>
 *
 * @author M
 * @since 2019-03-26
 */
@Controller
@RequestMapping("/clyjUserTouch")
public class ClyjUserTouchController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjUserTouchService clyjUserTouchService;
    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 我的分享-我的导师
     *
     * @param params
     * @return
     */
    @RequestMapping("/getUserShare")
    @ResponseBody
    public ApiResult getUserShare(@RequestBody JSONObject params) {
        String token = params.getString("token");
        if (token == null || token.equals("")) {
            return ApiResult.fail("请先登录");
        }
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        if (clyjUser == null) {
            return ApiResult.fail("登录已过期");
        }
        return this.clyjUserTouchService.getUserShare(clyjUser.getUserId());
    }

    /**
     * 查询用户所有上级
     *
     * @param params
     * @return
     */
    @RequestMapping("/getAllPUser")
    @ResponseBody
    public ApiResult getAllPUser(@RequestBody JSONObject params) {
        String token = params.getString("token");
        if (token == null || token.equals("")) {
            return ApiResult.fail("请先登录");
        }
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        if (clyjUser == null) {
            return ApiResult.fail("登录已过期");
        }
        //查询登录用户所属第一级上级
        QueryWrapper<ClyjUserTouch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", clyjUser.getUserId());
        List<ClyjUserTouch> clyjUserTouch = this.clyjUserTouchService.selectList(queryWrapper);
        if (clyjUserTouch == null || clyjUserTouch.size() == 0) {
            logger.debug("暂无上级");
            return ApiResult.success("暂无上级");
        }
        return ApiResult.success(clyjUserTouch);
    }

    /**
     * 邀约会员数量
     *
     * @param params
     * @return
     */
    @RequestMapping("/getcount")
    @ResponseBody
    public ApiResult getcount(@RequestBody JSONObject params) {
        String token = params.getString("token");
        if (token == null || token.equals("")) {
            return ApiResult.fail("请先登录");
        }
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        if (clyjUser == null) {
            return ApiResult.fail("登录已过期");
        }
        return this.clyjUserTouchService.getcount(clyjUser.getUserId());
    }

    /**
     * 个人二维码
     *
     * @param params
     * @return
     */
    @RequestMapping("/getInfo")
    @ResponseBody
    public ApiResult getInfo(@RequestBody JSONObject params) {
        String token = params.getString("token");
        if (token == null || token.equals("")) {
            return ApiResult.fail("请先登录");
        }
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        if (clyjUser == null) {
            return ApiResult.fail("登录已过期");
        }
        return this.clyjUserTouchService.getInfo(clyjUser.getUserId());
    }

    /**
     * 已邀请会员
     *
     * @param params
     * @return
     */
    @RequestMapping("/getInviteUser")
    @ResponseBody
    public ApiResult getInviteUser(@RequestBody JSONObject params) {
        String token = params.getString("token");
        if (token == null || token.equals("")) {
            return ApiResult.fail("请先登录");
        }
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        if (clyjUser == null) {
            return ApiResult.fail("登录已过期");
        }
        return this.clyjUserTouchService.getInviteUser(clyjUser.getUserId());
    }


    /**
     * 已邀请导购
     *
     * @param params
     * @return
     */
    @RequestMapping("/getInviteUserInfo")
    @ResponseBody
    public ApiResult getInviteUserInfo(@RequestBody JSONObject params) {
        String token = params.getString("token");
        if (token == null || token.equals("")) {
            return ApiResult.fail("请先登录");
        }
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        if (clyjUser == null) {
            return ApiResult.fail("登录已过期");
        }
        return this.clyjUserTouchService.getInviteUserInfo(clyjUser.getUserId());
    }

    /**
     * 订单过滤
     *
     * @param clyjGoodsOrderDetailsVo
     * @return
     */
    @PostMapping(value = "/testOrders")
    @ResponseBody
    public Map<String, Object> testOrder(@RequestBody ClyjGoodsOrderDetailsVo clyjGoodsOrderDetailsVo) {
        String token = clyjGoodsOrderDetailsVo.getToken();
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        return this.clyjUserTouchService.testOrder(clyjGoodsOrderDetailsVo, clyjUser);
    }

    /**
     * 订单过滤
     *
     * @param clyjGoodsOrderDetailsVo
     * @return
     */
    @PostMapping(value = "/testOrder")
    @ResponseBody
    public ApiResult<?> testOrders(@RequestBody ClyjGoodsOrderDetailsVo clyjGoodsOrderDetailsVo) {
        String token = clyjGoodsOrderDetailsVo.getToken();
        ClyjUser clyjUser = tokenUtils.Itoken(token);
        if (clyjUser == null) return ApiResult.fail("您好，你的账号已登陆失效，请重新登陆！");
        return this.clyjUserTouchService.testOrders(clyjGoodsOrderDetailsVo, clyjUser);
    }
}

